package com.example.qxfw.ybzz.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.qxfw.common.entity.zy.TourFcst;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.sql.Timestamp;
import java.util.List;

@Mapper
@DS("service")
public interface TourFcstMapper extends BaseMapper<TourFcst> {

    @Select("select timechar " +
            "from tour_fcst " +
            "where forecasttype='${forecastType}' and datechar='${dateChar}' ")
    List<String> selectTimeChar(String forecastType,Timestamp dateChar);

    @Select("select DateChar, TimeChar , forecasttype " +
            "from tour_fcst " +
            "order by DateChar desc, forecasttype desc " +
            "limit 1")
    TourFcst selectList1();

    @Select("select stationid, stationname, Longitude, Latitude, Weather1, WindS, WindD, MaxTemp, MinTemp, WindS, advice " +
            "from tour_fcst " +
            "where datechar='${}' and timechar='{1}' and (NTimes='{3}' or NTimes='{2}') and forecasttype = '{4}' " +
            "order by stationid,NTimes")
    List<TourFcst> selectList2(Timestamp dataChar,String timeChar,String nTimes1,String nTimes2,String fore);

    @Update("UPDATE tour_fcst SET " +
            "    stationid = #{stationid}, " +
            "    datechar = #{datechar}, " +
            "    timechar = #{timechar}, " +
            "    ntimes = #{ntimes}, " +
            "    longitude = #{longitude}, " +
            "    latitude = #{latitude}, " +
            "    weather1 = #{weather1}, " +
            "    maxtemp = #{maxtemp}, " +
            "    mintemp = #{mintemp}, " +
            "    winds = #{winds}, " +
            "    windd = #{windd}, " +
            "    rain = #{rain}, " +
            "    humid = #{humid}, " +
            "    cloud = #{cloud}, " +
            "    cloudlow = #{cloudlow}, " +
            "    advice = #{advice}, " +
            "    forecasttype = #{forecasttype} " +
            "WHERE  " +
            "    ntimes = #{ntimes} AND  " +
            "    stationname = #{stationname} AND  " +
            "    timechar = #{timechar} AND  " +
            "    forecasttype = #{forecasttype} ")
    int updateOneData(@Param("ntimes") int timeChar,
                      @Param("stationname") String stationname,
                      @Param("timechar") String timechar,
                      @Param("forecasttype") int forecasttype);


    @Select("SELECT stationid, stationname, longitude, latitude, " +
            "       min(case when humid is null then 0 else humid end) as humid, " +
            "       max(case when maxtemp is null then 0 else maxtemp end) as maxtemp, " +
            "       max(case when winds is null then '0' else winds end) as winds, " +
            "       sum(case when rain is null then 0 else rain end) as rain " +
            "FROM tour_fcst " +
            "WHERE datechar = #{timestamp} AND timechar = #{timeChar} AND ntimes <= 24 AND forecasttype = 12 " +
            "GROUP BY stationid, stationname, longitude, latitude ")
    List<TourFcst> selectByDatecharAndTimeChar(String timeChar, Timestamp timestamp);

}
